package com.kma.summer2012.dao;

import com.kma.summer2012.factory.DAOFactory;
import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.factory.SpringBeansFactory;
import com.kma.summer2012.model.CrumbsProcessHistory;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Администратор
 * Date: 15.01.13
 * Time: 18:57
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsProcessHistoryDAOImpl implements CrumbsProcessHistoryDAO {
    @Override
    public void add(CrumbsProcessHistory history) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(history);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void update(CrumbsProcessHistory history) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(history);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public CrumbsProcessHistory getById(int history_id) {
        Session session = null;
        CrumbsProcessHistory history = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            history = (CrumbsProcessHistory) session.get(CrumbsProcessHistory.class, history_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return history;
    }

    @Override
    public Collection<CrumbsProcessHistory> getAll() {
        Session session = null;
        List historys = new ArrayList<CrumbsProcessHistory>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            historys = session.createCriteria(CrumbsProcessHistory.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return historys;
    }

    @Override
    public void delete(CrumbsProcessHistory history) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(history);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }


    @Override
/* Collection<CrumbsProcessHistory>*/
    public void getHistoryForRequest(int requestID, int commenterID) {
        Session session = null;
        Disjunction disjunction = Restrictions.disjunction();

        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        List<CrumbsProcessHistory> history = new ArrayList<CrumbsProcessHistory>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();


            disjunction.add(Restrictions.eq("crumbsUserByRecieverId", factory.getCrumbsUserDAO().getById(commenterID)));
            disjunction.add(Restrictions.eq("crumbsUserBySenderId", factory.getCrumbsUserDAO().getById(commenterID)));
            history = session.createCriteria(CrumbsProcessHistory.class).add(Restrictions.eq("crumbsRequest", factory.getCrumbsRequestDAO().getById(requestID))).add(disjunction).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

            Set<CrumbsProcessHistory> h = new HashSet<CrumbsProcessHistory>();
            for (CrumbsProcessHistory historyItem : history) {
                h.add(historyItem);

            }
            factory.getCrumbsRequestDAO().getById(requestID).setCrumbsProcessHistories(h);

            Hibernate.initialize(factory.getCrumbsRequestDAO().getById(requestID).getCrumbsProcessHistories());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        //return history;
    }


    @Override
    public void getHistoryForUser(int requestID, int commenterID) {
        Session session = null;
        Disjunction disjunction = Restrictions.disjunction();

        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        List<CrumbsProcessHistory> history = new ArrayList<CrumbsProcessHistory>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();


            disjunction.add(Restrictions.eq("crumbsUserBySenderId", factory.getCrumbsUserDAO().getById(commenterID)));
            history = session.createCriteria(CrumbsProcessHistory.class).add(Restrictions.eq("crumbsRequest", factory.getCrumbsRequestDAO().getById(requestID))).add(Restrictions.eq("crumbsUserBySenderId", factory.getCrumbsUserDAO().getById(commenterID))).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

            Set<CrumbsProcessHistory> h = new HashSet<CrumbsProcessHistory>();
            for (CrumbsProcessHistory historyItem : history) {
                h.add(historyItem);

            }
            factory.getCrumbsRequestDAO().getById(requestID).setCrumbsProcessHistories(h);

            Hibernate.initialize(factory.getCrumbsRequestDAO().getById(requestID).getCrumbsProcessHistories());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        //return history;
    }

}
